package com.dfa.nypl.main;

import com.dfa.nypl.common.Constants;
import com.dfa.nypl.home.Home;
import com.dfa.nypl.location.Location;
import com.dfa.nypl.location.Statistics;
import com.dfa.nypl.news.News;
import com.dfa.nypl.tellafriend.TellAFriend;

import android.app.AlertDialog;
import android.app.TabActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.StaticLayout;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TabHost;
import android.widget.Toast;
import android.widget.TabHost.TabSpec;

public class TabHomeScreen extends TabActivity implements OnClickListener {

	public static TabHost tabHost;
	public static Button buttonCampaign;
	public static Button buttonTellFriend;
	public static Button buttonStatis;
	public static Button buttonNews;
	private LinearLayout campaignHomeLayout, campaignDonateLayout,
			campaignAdvocacyLayout, campaignLetterLayout;
	private LinearLayout helpSoFarLayout, viewLetterLayout;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		initWindowParameters();
		setContentView(R.layout.tab_home_screen);
		tabHost = (TabHost) findViewById(android.R.id.tabhost);
		buttonCampaign = (Button) findViewById(R.id.campaign);
		buttonTellFriend = (Button) findViewById(R.id.tell_a_friend);
		buttonStatis = (Button) findViewById(R.id.statistics);
		buttonNews = (Button) findViewById(R.id.news);
		buttonCampaign.setOnClickListener(this);
		buttonTellFriend.setOnClickListener(this);
		buttonStatis.setOnClickListener(this);
		buttonNews.setOnClickListener(this);
		setButtonCapaign();
		/* tid1 is firstTabSpec Id. Its used to access outside. */
		TabSpec firstTabSpec = tabHost.newTabSpec("tid1");
		TabSpec secondTabSpec = tabHost.newTabSpec("tid2");
		TabSpec thridTabSpec = tabHost.newTabSpec("tid3");
		TabSpec fourTabSpec = tabHost.newTabSpec("tid4");
		// TabSpec fiveTabSpec = tabHost.newTabSpec("tid3");
		// TabSpec sixTabSpec = tabHost.newTabSpec("tid3");
		// TabSpec sevenTabSpec = tabHost.newTabSpec("tid3");
		// TabSpec eightTabSpec = tabHost.newTabSpec("tid3");
		// TabSpec nightTabSpec = tabHost.newTabSpec("tid3");
		// TabSpec tenTabSpec = tabHost.newTabSpec("tid3");
		// TabSpec elevenTabSpec = tabHost.newTabSpec("tid3");
		// TabSpec twenTabSpec = tabHost.newTabSpec("tid3");
		// TabSpec thrityTabSpec = tabHost.newTabSpec("tid3");
		// TabSpec fourtyTabSpec = tabHost.newTabSpec("tid3");

		/* TabSpec setContent() is used to set content for a particular tab. */
		firstTabSpec.setIndicator("Home", getResources().getDrawable(
				R.drawable.icon));
		secondTabSpec.setIndicator("tell-a-friend", getResources().getDrawable(
				R.drawable.icon));
		thridTabSpec.setIndicator("Statistics", getResources().getDrawable(
				R.drawable.icon));
		fourTabSpec.setIndicator("News", getResources().getDrawable(
				R.drawable.icon));

		firstTabSpec.setContent(new Intent(this, Home.class));
		secondTabSpec.setContent(new Intent(this, TellAFriend.class));
		thridTabSpec.setContent(new Intent(this, Statistics.class));
		fourTabSpec.setContent(new Intent(this, News.class));

		/* Add tabSpec to the TabHost to display. */
		tabHost.addTab(firstTabSpec);
		tabHost.addTab(secondTabSpec);
		tabHost.addTab(thridTabSpec);
		tabHost.addTab(fourTabSpec);

		getTabHost().setCurrentTab(0);

		/** --------------------------------------------------------- **/

	}

	public void initWindowParameters() {
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
		this.getWindow().requestFeature(Window.FEATURE_PROGRESS);
	}

	public void closeApp() {
		final AlertDialog alertDialog = new AlertDialog.Builder(this).create();
		alertDialog.setTitle("Close Application");
		alertDialog.setIcon(R.drawable.icon);
		alertDialog.setMessage("Are you sure?");
		alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				alertDialog.dismiss();
				TabHomeScreen.this.finish();

			}
		});
		alertDialog.setButton2("Cancel", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				alertDialog.dismiss();
			}
		});
		alertDialog.show();
	}

	public static void setButtonCapaign() {
		buttonCampaign.setBackgroundResource(R.drawable.button_home);
		buttonTellFriend.setBackgroundResource(Color.TRANSPARENT);
		buttonStatis.setBackgroundResource(Color.TRANSPARENT);
		buttonNews.setBackgroundResource(Color.TRANSPARENT);
	}

	public static void setButtonTellAFriend() {
		buttonCampaign.setBackgroundResource(Color.TRANSPARENT);
		buttonTellFriend.setBackgroundResource(R.drawable.tell_a_friend);
		buttonStatis.setBackgroundResource(Color.TRANSPARENT);
		buttonNews.setBackgroundResource(Color.TRANSPARENT);
	}

	public static void setButtonLocation() {
		buttonCampaign.setBackgroundResource(Color.TRANSPARENT);
		buttonTellFriend.setBackgroundResource(Color.TRANSPARENT);
		buttonStatis.setBackgroundResource(R.drawable.location);
		buttonNews.setBackgroundResource(Color.TRANSPARENT);
	}

	public static void setButtonNews() {
		buttonCampaign.setBackgroundResource(Color.TRANSPARENT);
		buttonTellFriend.setBackgroundResource(Color.TRANSPARENT);
		buttonStatis.setBackgroundResource(Color.TRANSPARENT);
		buttonNews.setBackgroundResource(R.drawable.news);
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			// if (tabHost.getCurrentTab() == 0)
			// closeApp();

		}
		return true;
	}

	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.campaign:

			setButtonCapaign();
			tabHost.setCurrentTab(0);
			View view = tabHost.getCurrentView();
			campaignHomeLayout = (LinearLayout) view
					.findViewById(R.id.campaign_home_layout);
			campaignDonateLayout = (LinearLayout) view
					.findViewById(R.id.campaign_donate_layout);
			campaignAdvocacyLayout = (LinearLayout) view
					.findViewById(R.id.campaign_writer_layout1);
			campaignLetterLayout = (LinearLayout) view
					.findViewById(R.id.campaign_letter_layout);

			setHomeView();
			break;
		case R.id.tell_a_friend:
			setButtonTellAFriend();
			tabHost.setCurrentTab(1);
			break;
		case R.id.statistics:
			setButtonLocation();
			tabHost.setCurrentTab(2);
			View v1 = tabHost.getCurrentView();
			helpSoFarLayout = (LinearLayout) v1
					.findViewById(R.id.help_so_far_layout);
			viewLetterLayout = (LinearLayout) v1
					.findViewById(R.id.view_letter_by_branch_layout);
			setHelpSoFarView();
			break;
		case R.id.news:
			setButtonNews();
			tabHost.setCurrentTab(3);
			break;

		default:
			break;
		}
	}

	private void setHelpSoFarView() {
		viewLetterLayout.setVisibility(View.GONE);
		helpSoFarLayout.setVisibility(View.VISIBLE);
		Statistics.isHelpSoFarScreen = true;
	}

	public void setHomeView() {
		campaignHomeLayout.setVisibility(View.VISIBLE);
		campaignDonateLayout.setVisibility(View.GONE);
		campaignAdvocacyLayout.setVisibility(View.GONE);
		campaignLetterLayout.setVisibility(View.GONE);
		Home.codeBackScreen = 0;
	}

}
